<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseAdminController;
use App\Service\QuestionVersionService;

class QuestionVersionController extends BaseAdminController{
    protected $questionVersionService;

    public function _init() {
        $this->questionVersionService = new QuestionVersionService();
    }

    public function index(){
        $data = $this->questionVersionService->getList("*",[],[],['id'=>'desc']);
        return view("admin.question_version.index",compact(['data']));
    }

    public function create(){
        if($this->request->isMethod("GET")){
            return view("admin.question_version.create");
        }else{
            $data = $this->request->except("_token");
            $data['site_id'] = getCurrentSiteId();
            $this->validateRequest($this->request,"create");
            $result = $this->questionVersionService->create($data);
            if($result){
                return $this->success(7000,"添加成功");
            }else{
                return $this->error(7000,"添加失败");
            }
        }
    }
    public function update(){
        if($this->request->isMethod("GET")){
            $id = $this->request->input("id");
            $data = $this->questionVersionService->find($id);
            return view("admin.question_version.update",compact(['data']));
        }else{
            $data = $this->request->except("_token");
            $this->validateRequest($this->request,"create");
            $result = $this->questionVersionService->update($data['id'],$data);
            if($result){
                return $this->success(7000,"操作成功");
            }else{
                return $this->error(7000,"操作失败");
            }
        }
    }
    public function destroy($id) {
        $result = $this->questionVersionService->delete($id);
        if($result){
            return $this->success($id, "删除成功");
        }else{
            return $this->error($result,"删除失败");
        }
    }
    public function usable(){
        $data = $this->request->except("_token");
        $result = $this->questionVersionService->update($data['id'],['usable'=>$data['usable']]);
        if($result){
            $res['code'] = 1;
            $res['msg'] = "操作成功";
        }else{
            $res['code'] = -1;
            $res['msg'] = "操作失败";
        }
        return $res;
    }
}
